﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.Permission.Dtos.UserRelation;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 用户管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.21 
    /// </summary>
    [Route("api/Permission/[controller]")]
    public class UserRelationController : BaseApiController
    {
        private UserRelationBll userRelation = new UserRelationBll();
        private UserOnlineBll userOnlineBll = new UserOnlineBll();

        #region GET
        /// <summary>
        /// 获取岗位/角色成员列表
        /// </summary>
        /// <param name="objectid">岗位id或角色id</param>
        /// <returns></returns>
        [HttpGet("{objectId}")]
        public async Task<ResponseResult> GetList(string objectid)
        {
            var data = await userRelation.GetListByObjectId(objectid);
            var output = data.Select(x => x.UserId).ToList<string>();
            return Success(new { ids = output });
        }
        #endregion

        #region POST
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="objectId">功能主键</param>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPost("{objectId}")]
        public async Task<ResponseResult> Create(string objectId, [FromBody] UserRelationCrInput input)
        {
            List<UserRelationEntity> entityList = new List<UserRelationEntity>();
            var oldUserIds = (await userRelation.GetListByObjectId(objectId)).Select(x => x.UserId).ToList();
            var newUserIds = new List<string>(input.userIds).Union(oldUserIds).ToList();
            #region 修改用户关系数据
            await userRelation.Save(objectId, input.objectType, input.userIds);
            #endregion
            #region 修改用户数据
            await userRelation.UpdateUserRelation(oldUserIds, input.userIds.ToList(), objectId, input.objectType);
            #endregion 
            #region 清除用户缓存
            var onlineUserIds = (await userOnlineBll.GetList()).Select(x => x.userId).ToList();
            foreach (var item in onlineUserIds)
            {
                if (oldUserIds.Contains(item) || newUserIds.Contains(item))
                {
                    await userOnlineBll.Delete(item);
                    CacheFactory.Cache.Remove(CacheKey.USERAUTHORIZE + item);
                }
            }
            #endregion
            return Success("保存成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResponseResult> Delete([FromBody] UserRelationDelInput input)
        {
            await userRelation.Delete(input.ids);
            return Success("删除成功");
        }
        #endregion
    }
}
